//
//  NewsListUISegmentedView.m
//  ShowYork
//
//  Created by hxh on 2021/1/18.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "HomePageListSegmentedView.h"
#import "HomePageListSegmentedCell.h"

typedef void(^doneBlock)(NSInteger index);


@interface HomePageListSegmentedView ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,copy)doneBlock done;
@end

@implementation HomePageListSegmentedView

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock{
    return [self initWithTitleArray:@[] completeBlock:completeBlock];
}
- (instancetype)initWithTitleArray:(NSArray <HomePageTagModel *>*)itemArray completeBlock:(void (^)(NSInteger))completeBlock{

    if (self = [super init]) {
        _itemArray = itemArray;
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, 44);
    self.currentIndex = 0;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.collectionView];
    
    [self.collectionView registerClass:[HomePageListSegmentedCell class] forCellWithReuseIdentifier:@"HomePageListSegmentedCell"];
    self.backgroundColor = [UIColor whiteColor];
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomePageListSegmentedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomePageListSegmentedCell" forIndexPath:indexPath];
    
    [cell setModel:self.itemArray[indexPath.item]];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomePageTagModel *model = self.itemArray[indexPath.item];
    CGSize size = [RPAppManager getTextSizeWithText:model.topicName font:font(14) maxSize:CGSizeMake(kScreenW,MAXFLOAT)];
    return CGSizeMake(ceil(size.width + 24), 44);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 5, 0, 8);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4.0f;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  
    
    if (indexPath.item == self.currentIndex) {
        return;
    }
    
    UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
    CGPoint cellCenter = attributes.center;
    
    CGFloat scrollx = cellCenter.x - self.collectionView.width * 0.5;
    if (scrollx < 0) {
        scrollx = 0;
    }
    
    if (self.collectionView.contentSize.width - self.collectionView.width < 0) {
        scrollx = 0;
    }else{
        if (scrollx > self.collectionView.contentSize.width - self.collectionView.width) {
            scrollx = self.collectionView.contentSize.width - self.collectionView.width + 16;
        }
    }
    [self.collectionView setContentOffset:CGPointMake(scrollx, 0) animated:YES];
    
    
    
    if (indexPath.item != self.currentIndex) {
        HomePageTagModel *lastModel = self.itemArray[self.currentIndex];
        lastModel.isSelected = NO;
        
        HomePageTagModel *currentModel = self.itemArray[indexPath.item];
        currentModel.isSelected = YES;
        
        _currentIndex = indexPath.item;
  
        [self.collectionView reloadData];
        
        if (self.done) {
            self.done(indexPath.item);
        }
    }
}

- (void)setCurrentIndex:(NSInteger)currentIndex{
    if (currentIndex != self.currentIndex) {
        HomePageTagModel *lastModel = self.itemArray[self.currentIndex];
        lastModel.isSelected = NO;
        
        HomePageTagModel *currentModel = self.itemArray[currentIndex];
        currentModel.isSelected = YES;
        
        [self.collectionView reloadData];
        
    }
    _currentIndex = currentIndex;
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentIndex inSection:0];
    
    UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
    CGPoint cellCenter = attributes.center;
 
    CGFloat scrollx = cellCenter.x - self.collectionView.width * 0.5;
    if (scrollx < 0) {
        scrollx = 0;
    }
    if (self.collectionView.contentSize.width - self.collectionView.width < 0) {
        scrollx = 0;
    }else{
        if (scrollx > self.collectionView.contentSize.width - self.collectionView.width) {
            scrollx = self.collectionView.contentSize.width - self.collectionView.width + 16;
        }
    }
    [self.collectionView setContentOffset:CGPointMake(scrollx, 0) animated:YES];
    
}


- (void)setItemArray:(NSArray<HomePageTagModel *> *)itemArray{
    
    
    [itemArray enumerateObjectsUsingBlock:^(HomePageTagModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            obj.isSelected = YES;
        }
    }];
    _itemArray = itemArray;
    [self.collectionView reloadData];
}

@end
